﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

using CE.Database;
using CE.CarInsurance;
using CE.Configuration;
using CE.Mail;
using CE.Global;
using CE.User;

public partial class ViewResultHistory : MyLanguageBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
            {
                UserResultDataBind();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
        else
        {
            lblNoResultMessage.Text = "";
        }
    }

    protected void ibtRunNewQuote_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("quote.aspx");
    }

    private void UserResultDataBind()
    {
        dlCarResults.DataSource = CreateDatasource("CI", 5);
        dlCarResults.DataBind();

        if (dlCarResults.Items.Count == 0)
        {
            lblNoResultMessage.Text = Resources.Menu.ViewHistory;
        }
        else
        {
            lblNoResultMessage.Text = "";
        }

        General.ClearAllSession(this.Page, "AL");
    }
    

    private ICollection CreateDatasource(string ResultType, int ResultRowCount)
    {
        UserResultHistoryDB _resultdb = new UserResultHistoryDB();
        DataTable _dt = new DataTable("ResultTable");
        _dt = _resultdb.GetResultHistoryTable(Convert.ToInt64(Session[General.UserId].ToString()), ResultType);
        DataView _dv = new DataView(_dt);
        return _dv;
    }

    protected void dlCarResults_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Int64 _NomGG = 0;
            Int64 CustomerId = 0;
            if (Session[CE.Global.General.UserId] != null)
            {
                CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
            }
            Int64 CECarId = Convert.ToInt64(((DataRowView)(e.Item.DataItem)).Row["ID1"].ToString());
            DataSet _dsNonGG = new DataSet();
            _dsNonGG = GetCarDetails(CECarId, CustomerId);
            if (_dsNonGG != null && _dsNonGG.Tables.Count > 2 && _dsNonGG.Tables[2].Rows.Count > 0)
            {
                _NomGG = 1;
            }
            Button ibtTempResult = (Button)e.Item.FindControl("ibtShowResult");
            Button ibtTempDelete = (Button)e.Item.FindControl("ibtDeleteResult");

            Button ibtRerunQuotes = (Button)e.Item.FindControl("ibtRerunQuotes"); 
            Button ibtDeleteResult2 = (Button)e.Item.FindControl("ibtDeleteResult2"); 
        
            Label lblQuoteTitle = (Label)e.Item.FindControl("lblQuoteTitle");
            Label lblInsuranceType = (Label)e.Item.FindControl("lblInsuranceType");
            Label lblGarageType = (Label)e.Item.FindControl("lblGarageType");
            Label lblCompleteDate = (Label)e.Item.FindControl("lblCompleteDate");
            Label lblPolicyStartDate = (Label)e.Item.FindControl("lblPolicyStartDate");
            Label lblStatus = (Label)e.Item.FindControl("lblStatus");

            Int64 _resultId = 0;
            string _rowguid = "";
            string _status = "";

            DateTime _dtDateComplete = General.GetUTCDateTime();
            DateTime _dtPolicyStartDate = General.GetUTCDateTime();
            string _resulttype = "CI";
            bool _iscomplete = false;
            HtmlGenericControl _divPartial = (HtmlGenericControl)e.Item.FindControl("divPartialButton");
            HtmlGenericControl _divComplete = (HtmlGenericControl)e.Item.FindControl("divCompleteButton");
            HtmlGenericControl _divPartial2 = (HtmlGenericControl)e.Item.FindControl("divPartialButton2");

            if (_dsNonGG != null && _dsNonGG.Tables.Count > 2 && _dsNonGG.Tables[2].Rows.Count > 0)
            {
                _resultId = 0;
                _rowguid = "0";
                _status = "Complete";

                _dtDateComplete = Convert.ToDateTime(((DataRowView)(e.Item.DataItem)).Row["CreatedDate"].ToString());
                if (_dsNonGG.Tables[2].Rows[0]["CoverageStart"] != DBNull.Value && _dsNonGG.Tables[2].Rows[0]["CoverageStart"].ToString() != string.Empty)
                {
                    _dtPolicyStartDate = Convert.ToDateTime(_dsNonGG.Tables[2].Rows[0]["CoverageStart"].ToString());
                    lblPolicyStartDate.Text = _dtPolicyStartDate.ToString("dd/MMM/yyyy");
                }
                _resulttype = ((DataRowView)(e.Item.DataItem)).Row["ResultType"].ToString();
                _iscomplete = true;
            }
            else
            {
                _resultId = Convert.ToInt64(((DataRowView)(e.Item.DataItem)).Row["UserResultId"].ToString());
                _rowguid = ((DataRowView)(e.Item.DataItem)).Row["RowGUID"].ToString();
                _status = ((DataRowView)(e.Item.DataItem)).Row["Status"].ToString();

                _dtDateComplete = Convert.ToDateTime(((DataRowView)(e.Item.DataItem)).Row["CreatedDate"].ToString());
                _resulttype = ((DataRowView)(e.Item.DataItem)).Row["ResultType"].ToString();
                _iscomplete = Convert.ToBoolean(((DataRowView)(e.Item.DataItem)).Row["IsComplete"].ToString());
            }
         
            if (_status.ToUpper() == "COMPLETE")
            {
                lblStatus.Text = _status + "d Quote";
                lblStatus.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblStatus.Text = _status + " Quote";
                lblStatus.ForeColor = System.Drawing.Color.Red;
            }

            lblCompleteDate.Text = _dtDateComplete.ToString("dd/MMM/yyyy");
            
            if (_resulttype == "CI")
            {
                string _cardescription = ((DataRowView)(e.Item.DataItem)).Row["Info1"].ToString();
                string _insuranceype = ((DataRowView)(e.Item.DataItem)).Row["Info2"].ToString();
                string _garageType = ((DataRowView)(e.Item.DataItem)).Row["Info3"].ToString();
                string _vehicleUsage = ((DataRowView)(e.Item.DataItem)).Row["Info4"].ToString();
                Int64 _ID1 = Convert.ToInt64(((DataRowView)(e.Item.DataItem)).Row["ID1"].ToString());
                Int64 _ID2 = Convert.ToInt64(((DataRowView)(e.Item.DataItem)).Row["ID2"].ToString());
                Int64 _ID3 = Convert.ToInt64(((DataRowView)(e.Item.DataItem)).Row["ID3"].ToString());
                Int64 _ID5 = Convert.ToInt64(((DataRowView)(e.Item.DataItem)).Row["ID5"].ToString());
                string _refno = ((DataRowView)(e.Item.DataItem)).Row["Info10"].ToString();
                string _carmodel = "";

                if (_cardescription != string.Empty)
                {
                    _carmodel = _cardescription;
                }

                bool _requestStatus = true;
                DateTime _todayDate = new DateTime(General.GetUTCDateTime().Year, General.GetUTCDateTime().Month, General.GetUTCDateTime().Day);
                _todayDate = _todayDate.AddDays(-14);
                if (_todayDate.CompareTo(_dtDateComplete) > 0)
                {
                    _requestStatus = false;
                }
                else
                {
                    _requestStatus = true;
                }

                bool _commenceStatus = true;
                DateTime _today = new DateTime(General.GetUTCDateTime().Year, General.GetUTCDateTime().Month, General.GetUTCDateTime().Day);
                if (_today.CompareTo(_dtPolicyStartDate) >= 0)
                {
                    _commenceStatus = false;
                }
                else
                {
                    _commenceStatus = true;
                }

                if (_garageType == "1")
                {
                    lblGarageType.Text = Resources.CarInsurance.Garage_Authorized;
                }
                else if (_garageType == "2")
                {
                    lblGarageType.Text = Resources.CarInsurance.Garage_Dealer;
                }

                if (_insuranceype == "10")
                {
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOne;
                }
                else if (_insuranceype == "11")
                {
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOneLowCost;
                }
                else if (_insuranceype == "21")
                {
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassTwoPlus;
                }
                else if (_insuranceype == "30")
                {
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThree;
                }
                else if (_insuranceype == "31")
                {
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThreePlus;
                }

                if (_iscomplete == true)
                {
                    string _providerId = "0";
                    if (_NomGG == 1)
                    {
                        _providerId = _dsNonGG.Tables[2].Rows[0]["ProviderCompanyId"].ToString();
                    }
                    else
                    {
                        MyDatabase _db = new MyDatabase();
                        string strSql = "SELECT ProviderCompanyId FROM [tblCarInsuranceSelectItemTH] WHERE CarId = " + _ID1 + " AND [Status] = 1";
                        _providerId = _db.ExecuteScalarSqlStatement(strSql).ToString();
                    }

                    string _packagename = "";
                    if (_ID5 > 0)
                    {
                        //Car ID
                        MyDatabase _db = new MyDatabase();
                         
                       string strSql = "SELECT CoverTypeDesc FROM [tblCarInsuranceTempResultTH] WHERE CITempResultId = " + _ID5.ToString() + " AND CarId = " + _ID1.ToString();
                       object objpgname = _db.ExecuteScalarSqlStatement(strSql);
                       if (objpgname != null)
                           _packagename = objpgname.ToString();
                       
                    }

                    _refno = General.GetRefNumber(_ID1);
                    if (_packagename != "GluayGluay.com Special")
                    {
                        if (_providerId == "11")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : AZC" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "12")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : MSI" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "16")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : AXA" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "29")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : LMG" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "31")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : CTS" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "32")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : BKI" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "33")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : MTI" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "35")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : KPI" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "39")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : MIR" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "41")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : KSK" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "43")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : SMK" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "45")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : VRY" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "46")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : TMS" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "50")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : SCS" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "58")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : TVV" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                        else if (_providerId == "65")
                        {
                            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : DPY" + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                        }
                    }
                    else
                    {
                        lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : GG" + _providerId.ToString() + _refno + " - " + Resources.CarInsurance.CarInsuranceList + " " + _carmodel;
                    }

                    ibtTempResult.Enabled = false;

                    ibtTempDelete.Enabled = false;

                    _divPartial.Visible = false;
                    _divComplete.Visible = true;

                    _divPartial2.Visible = false; 
                    ibtRerunQuotes.Enabled = false;
                }
                else
                {
                    if (_requestStatus == true)
                    {
                        _divPartial.Visible = true;
                        _divPartial2.Visible = false;
                    }
                    else
                    {
                        _divPartial.Visible = false;
                        _divPartial2.Visible = true;
                    }

                    if (_commenceStatus == true)
                    {
                        _divPartial.Visible = true;
                        _divPartial2.Visible = false;
                    }
                    else
                    {
                        _divPartial.Visible = false;
                        _divPartial2.Visible = true;
                    }

                    lblQuoteTitle.Text = _carmodel;


                    if (_ID2 != 0)
                    {
                        ibtTempDelete.Attributes.Add("OnClick", "javascript:if (confirm('Are you sure to delete \"" + _carmodel + "\"?')) { this.className='button_w_processing';return true; } else { return false; }");
                        ibtDeleteResult2.Attributes.Add("OnClick", "javascript:if (confirm('Are you sure to delete \"" + _carmodel + "\"?')) { this.className='button_w_processing';return true; } else { return false; }");
                    }
                }
            }
        }
    }

    protected void dlCarResults_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.Item != null && e.Item.ItemIndex >= 0)
        {
            Int64 _resultId = Convert.ToInt64(dlCarResults.DataKeys[e.Item.ItemIndex].ToString());

            Int64 _userId = Convert.ToInt64(Session[General.UserId].ToString());

            UserResultHistory _resulthis = new UserResultHistory();
            UserResultHistoryDB _resulthisdb = new UserResultHistoryDB();

            _resulthis = _resulthisdb.GetResultHistory(_userId, _resultId);
            if (_resulthis != null)
            {
                if (e.CommandName != "ShowSavedQuote")
                {
                    if (_resulthis.ResultType == "CI")
                    {
                        if (_resulthis.ID1 != null)
                        {
                            bool _iscomplete = true;
                            MyDatabase _db = new MyDatabase();
                            string strSql = "SELECT IsComplete FROM [tblUserResult] WHERE ResultType = 'CI' AND ID1 = " + _resulthis.ID1.ToString();
                            _iscomplete = Convert.ToBoolean(_db.ExecuteScalarSqlStatement(strSql).ToString());
                            if (_iscomplete == true)
                            {
                                Session["ViewCIResultId"] = _resultId.ToString();
                                Response.Redirect("Complete-Quote.aspx");
                            }
                        }
                    }
                }

                if (e.CommandName == "ShowResult")
                {
                    if (_resulthis.ResultType == "CI")
                    {
                        Session[General.CarId] = _resulthis.ID1.ToString();
                        Session[General.CarPolicyId] = _resulthis.ID2.ToString();
                        Session[General.IsCarRestoreResult] = "1";
                        Response.Redirect("quote_result.aspx");
                    }
                }
                else if (e.CommandName == "ReRunQuotes")
                {
                    if (_resulthis.ResultType == "CI")
                    {
                        Session[General.CarId] = _resulthis.ID1.ToString();
                        Session[General.CarPolicyId] = _resulthis.ID2.ToString();
                        Session[General.IsCarRestoreResult] = null;
                        Response.Redirect("quote_result.aspx");
                    }
                }
                else if (e.CommandName == "DeleteResult")
                {
                    _resulthisdb.Delete(_userId, _resultId);
                    General.ClearAllSession(Page, "ALL");
                    UserResultDataBind();
                }
                else if (e.CommandName == "ShowSavedQuote")
                {
                    if (_resulthis.ResultType == "CI")
                    {
                        Session["ViewCIResultId"] = _resultId.ToString();
                        Response.Redirect("Complete-Quote.aspx");
                    }
                }
            }
        }
    }

    public DataSet GetCarDetails(Int64 CarId, Int64 CustomerId)
    {
        MyDatabase _db = new MyDatabase();
        string _spName = "[sprocCXCRMNONGGPackageCarDetailsSelectedItemByCarId]";

        SortedList _spara = new SortedList();
        _spara.Add("@CarId", CarId);
        _spara.Add("@CustomerId", CustomerId);

        DataSet _ds = _db.GetDataSet(_spName, _spara);

        return _ds;
    }

}
